//
//  DKLoginRegisterViewController.m
//  DanKU
//
//  Created by 吴斌 on 2023/9/18.
//

#import "DKLoginRegisterViewController.h"
#import "DKLoginRegisterNameView.h"
#import "DKLoginRegisterAccountView.h"
#import "DKLoginAccountView.h"
#import "DKLoginMsgCodeView.h"
#import "DKRegisterVoiceView.h"
#import "DKLoginHelpView.h"
#import "DKLoginAgreementAlertView.h"
#import "DKRegisterPwdViewController.h"
#import "DKRegisterHttp.h"
#import "DKTabBarViewController.h"
#import "DKLoginVoiceAlertView.h"
#import "DKFaqAlertView.h"
@interface DKLoginRegisterViewController ()<TTTAttributedLabelDelegate>
@property (nonatomic, strong)DKLoginAccountView * dk_LoginAccountView;
@property (nonatomic, strong)DKLoginMsgCodeView * dk_LoginMsgCodeView;
@property (nonatomic, strong)UILabel * dk_LoginAlertLab;
@property (nonatomic, strong)DKRegisterVoiceView *dk_RegisterVoiceView;
@property (nonatomic, strong)UIButton * dk_RegisterNextBtn;
@property (nonatomic, strong)UIButton * dk_RegisterAgreementBtn;
@end

@implementation DKLoginRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    // Do any additional setup after loading the view.
    [self dk_creatView];
    [self showSureAgreemnetView:YES];
}
- (void)dk_creatView{
    WeakSelf(self)
    //log名称
    DKLoginRegisterNameView *loginRegisterNameView = [[DKLoginRegisterNameView alloc] init];
    [self.view addSubview:loginRegisterNameView];
    [loginRegisterNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(76)+DK_SafeAreaTop));
        make.height.equalTo(@(DK_IsWidthScale(40)));
    }];
    //注册提醒
    UILabel *dk_RegisterAlertLab = [[UILabel alloc] init];
    dk_RegisterAlertLab.textColor = DK_Hex(@"#333333");
    dk_RegisterAlertLab.font = DK_FONT_Medium(17);
    dk_RegisterAlertLab.text = @"Register with mobile number";
    [self.view addSubview:dk_RegisterAlertLab];
    [dk_RegisterAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(24)));
        make.top.equalTo(loginRegisterNameView.mas_bottom).offset(DK_IsWidthScale(30));
    }];
    //去账号登录
    DKLoginRegisterAccountView *dk_LoginRegisterAccountView = [[DKLoginRegisterAccountView alloc] init];
    [self.view addSubview:dk_LoginRegisterAccountView];
    [dk_LoginRegisterAccountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(dk_RegisterAlertLab.mas_bottom).offset(DK_IsWidthScale(15));
        make.height.equalTo(@(DK_IsWidthScale(21)));
    }];
    dk_LoginRegisterAccountView.goAccountLoginBlock = ^{//去账号登录
        [weakSelf.navigationController popToRootViewControllerAnimated:YES];
    };
    //账号
    self.dk_LoginAccountView = [[DKLoginAccountView alloc] init];
    self.dk_LoginAccountView.smsType = @"register";
    self.dk_LoginAccountView.dk_LoginAccountTextField.text = self.dk_registerPhone;
    self.dk_LoginAccountView.dk_LoginAccountTextField.enabled = NO;
    [self.view addSubview:self.dk_LoginAccountView];
    [self.dk_LoginAccountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(30)));
        make.right.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(dk_LoginRegisterAccountView.mas_bottom).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    self.dk_LoginAccountView.goCheckAcountBlock = ^(BOOL isPhoneNull) {//检查输入手机号是否是空
        [weakSelf makeNextStatus];
    };
    self.dk_LoginAccountView.goAccountResetBlock = ^{//重新选择手机号
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    //验证码
    self.dk_LoginMsgCodeView = [[DKLoginMsgCodeView alloc] init];
    [self.view addSubview:self.dk_LoginMsgCodeView];
    [self.dk_LoginMsgCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(30)));
        make.right.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_LoginAccountView.mas_bottom).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    self.dk_LoginMsgCodeView.goCheckMsgCodeBlock = ^(BOOL isPhoneCodeNull) {
        [weakSelf makeNextStatus];
        [weakSelf requestUserBrhavRecord:DK_ActionVCode_Input withParmDic:@{}];
    };
    self.dk_LoginMsgCodeView.StartEditPhoneBlock = ^{//开始编辑
        [weakSelf requestUserBrhavRecord:DK_ActionVCode_Focus withParmDic:@{}];
    };
    //提示
    self.dk_LoginAlertLab = [[UILabel alloc] init];
    self.dk_LoginAlertLab.textColor = DK_Hex(@"#ED4342");
    self.dk_LoginAlertLab.font = DK_FONT_Regular(10);
    self.dk_LoginAlertLab.hidden = YES;
    self.dk_LoginAlertLab.numberOfLines = 0;
    [self.view addSubview:self.dk_LoginAlertLab];
    [self.dk_LoginAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_LoginMsgCodeView.mas_bottom).offset(DK_IsWidthScale(5));
        make.height.equalTo(@(DK_IsWidthScale(28)));
    }];
    //语音验证码
    self.dk_RegisterVoiceView = [[DKRegisterVoiceView alloc] init];
    [self.view addSubview:self.dk_RegisterVoiceView];
    [self.dk_RegisterVoiceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_LoginMsgCodeView.mas_bottom).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(17)));
    }];
    self.dk_RegisterVoiceView.goRegisterVoiceBlock = ^{//语音验证码
//        if([weakSelf.dk_LoginAccountView isCanSendCode]){
            [weakSelf.dk_LoginAccountView sendVoiceCode];
//        }else{
//            [DKAlertHelper showMessage:@"The verification code sending is busy, please try again later"];
//        }
        
//        [DKLoginVoiceAlertView dk_ShowVoiceAlertViewNextCompleteIsDismiss:YES withVoice:^{
//            [weakSelf.dk_LoginAccountView sendVoiceCode];
//        } WithSms:^{
//            [weakSelf.dk_LoginAccountView sendLoginMsgCode];
//        }];
    };
    //协议
    self.dk_RegisterAgreementBtn = [[UIButton alloc] init];
    [self.dk_RegisterAgreementBtn setBackgroundImage:[UIImage imageNamed:@"dk_register_noSelect"] forState:UIControlStateNormal];
    [self.dk_RegisterAgreementBtn setBackgroundImage:[UIImage imageNamed:@"dk_register_Select"] forState:UIControlStateSelected];
    [self.dk_RegisterAgreementBtn addTarget:self action:@selector(goAgreeAgreenment:) forControlEvents:UIControlEventTouchUpInside];
    self.dk_RegisterAgreementBtn.selected = YES;
    [self.view addSubview:self.dk_RegisterAgreementBtn];
    [self.dk_RegisterAgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(36)));
        make.width.equalTo(@(DK_IsWidthScale(16)));
        make.height.equalTo(@(DK_IsWidthScale(16)));
        make.bottom.equalTo(@(-DK_IsWidthScale(77)-DK_SafeAreaBottom));
    }];
    TTTAttributedLabel * dk_RegisterAgreementLab = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
    dk_RegisterAgreementLab.numberOfLines = 0;
    [self.view addSubview:dk_RegisterAgreementLab];
    [dk_RegisterAgreementLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_RegisterAgreementBtn.mas_right).offset(DK_IsWidthScale(6));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_RegisterAgreementBtn.mas_top).offset(-DK_IsWidthScale(1));
    }];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"Account login is deemed to agree with the《Privacy Agreement》and the 《Registration Agreement》"];
    [attStr addAttribute:NSFontAttributeName value:DK_FONT_Regular(12) range:NSMakeRange(0,attStr.length)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DK_Hex(@"#333333") range:NSMakeRange(0,attStr.length)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DK_Hex(@"#4566FF") range:NSMakeRange(41,19)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DK_Hex(@"#4566FF") range:NSMakeRange(68,attStr.length-68)];
    dk_RegisterAgreementLab.delegate = self;
    dk_RegisterAgreementLab.attributedText = attStr;
    dk_RegisterAgreementLab.enabledTextCheckingTypes = NSTextCheckingTypeLink;
    dk_RegisterAgreementLab.linkAttributes = @{NSForegroundColorAttributeName:DK_Hex(@"#4566FF")};
    [dk_RegisterAgreementLab addLinkToURL:[NSURL URLWithString:DK_PrivacyAgreement] withRange:NSMakeRange(41,19)];
    [dk_RegisterAgreementLab addLinkToURL:[NSURL URLWithString:DK_RegisterAgreement] withRange:NSMakeRange(68,attStr.length-68)];
    //help
    DKLoginHelpView *dk_LoginHelpView = [[DKLoginHelpView alloc] init];
    [self.view addSubview:dk_LoginHelpView];
    [dk_LoginHelpView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(dk_RegisterAgreementLab.mas_top).offset(-DK_IsWidthScale(73));
        make.height.equalTo(@(DK_IsWidthScale(17)));
    }];
    dk_LoginHelpView.goRegisterHelpBlock = ^{
        [DKPublicHttp apiGetFAQForAPI:@{} complete:^(NSString * _Nonnull faq) {
            [DKFaqAlertView dk_ShowFaqAlertViewComplete:faq complete:^{
                
            }];
        }];
    };
    //next
    self.dk_RegisterNextBtn = [[UIButton alloc] init];
    [self.dk_RegisterNextBtn setTitle:@"Next" forState:UIControlStateNormal];
    [self.dk_RegisterNextBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateDisabled];
    [self.dk_RegisterNextBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    self.dk_RegisterNextBtn.titleLabel.font = DK_FONT_Semibold(16);
    [self.dk_RegisterNextBtn setBackgroundImage:DK_GetImage(@"dk_registerNextSelectBg") forState:UIControlStateNormal];
    [self.dk_RegisterNextBtn setBackgroundImage:DK_GetImage(@"dk_registerNextBg") forState:UIControlStateDisabled];
    self.dk_RegisterNextBtn.enabled = NO;
    [self.dk_RegisterNextBtn addTarget:self action:@selector(goLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.dk_RegisterNextBtn];
    [self.dk_RegisterNextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.bottom.equalTo(dk_LoginHelpView.mas_top).offset(-DK_IsWidthScale(10));
    }];
}
- (void)showSureAgreemnetView:(BOOL)isSendCode{
    WeakSelf(self)
    [DKLoginAgreementAlertView dk_ShowLoginAgreementAlertViewNext:self.view Complete:^{
        weakSelf.dk_RegisterAgreementBtn.selected = YES;
        //自动发送短信验证
        if(isSendCode){
            [self.dk_LoginAccountView sendLoginMsgCode];
        }
    } WithGoUrl:^(NSString * _Nonnull url) {
//        DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] init];
//        dk_WebViewCtrl.webUrl = url;
        DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] initWithUrl:url withIsNavBar:YES];

        [weakSelf.navigationController pushViewController:dk_WebViewCtrl animated:YES];
    }];
}
- (void)makeShowAlert:(NSString *)alert{
    self.dk_LoginAlertLab.hidden = NO;
    self.dk_LoginAlertLab.text = @"The verification has expired/is incorrect. Please obtain a new code and try again.";
    [self.dk_RegisterVoiceView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.dk_LoginMsgCodeView.mas_bottom).offset(DK_IsWidthScale(50));
        make.height.equalTo(@(DK_IsWidthScale(17)));
    }];
}
- (void)makeNextStatus{
    if(self.dk_LoginAccountView.dk_LoginAccountTextField.text.length > 0&&([self.dk_LoginMsgCodeView getLoginCode].length > 0)&&self.dk_RegisterAgreementBtn.selected){
        self.dk_RegisterNextBtn.enabled = YES;
    }else{
        self.dk_RegisterNextBtn.enabled = NO;
    }
}
//选择协议
- (void)goAgreeAgreenment:(UIButton *)sender{
    sender.selected = !sender.isSelected;
    [self makeNextStatus];
}
- (void)goLogin{
    WeakSelf(self)
    //是否为空
    if(self.dk_LoginAccountView.dk_LoginAccountTextField.text.length <= 0){
        [DKAlertHelper showMessage:@"Please enter your phone number"];
        return;
    }
    //是否为空
    if([self.dk_LoginMsgCodeView getLoginCode].length <= 0){
        [DKAlertHelper showMessage:@"Please enter Verification code"];
        return;
    }
    NSMutableDictionary * parmDic = [[NSMutableDictionary alloc] init];
    [parmDic setObject:[self.dk_LoginMsgCodeView getLoginCode] forKey:@"vcode"];
    [self requestUserBrhavRecord:DK_ActionClick withParmDic:parmDic];
    
    NSMutableDictionary *registerUserDic = [[NSMutableDictionary alloc] init];
    NSString *loginCode = [self.dk_LoginMsgCodeView getLoginCode];
    [registerUserDic setValue:self.dk_LoginAccountView.dk_LoginAccountTextField.text forKey:@"loginName"];
    [registerUserDic setValue:loginCode forKey:@"vcode"];
    [DKRegisterHttp apiRegisterUserForAPI:registerUserDic complete:^{
        NSMutableDictionary * parmDic = [[NSMutableDictionary alloc] init];
        [parmDic setObject:loginCode forKey:@"vcode"];
        [weakSelf requestUserBrhavRecord:DK_ActionSave withParmDic:parmDic];
        
        [DKPublicHttp apiUserRiskForAPI:@"REGISTERED" complete:^{
            
        } fail:^{
            
        }];
        //进入设置密码页面
        DKRegisterPwdViewController *dk_RegisterPwdViewCtrl = [[DKRegisterPwdViewController alloc] init];
        dk_RegisterPwdViewCtrl.dk_registerPhone = weakSelf.dk_LoginAccountView.dk_LoginAccountTextField.text;
        [weakSelf.navigationController pushViewController:dk_RegisterPwdViewCtrl animated:YES];
    } failure:^(NSString * _Nonnull msg) {
        [weakSelf makeShowAlert:msg];
    }];
    
//    DKRegisterPwdViewController * dk_RegisterPwdViewCtrl = [[DKRegisterPwdViewController alloc] init];
//    dk_RegisterPwdViewCtrl.dk_registerPhone = self.dk_LoginAccountView.dk_LoginAccountTextField.text;
//    [self.navigationController pushViewController:dk_RegisterPwdViewCtrl animated:YES];
}
- (void)requestUserBrhavRecord:(NSString *)action withParmDic:(NSDictionary *)parmDic{
    [DKPublicHttp apiUserBrhavRecordForAPI:parmDic withPageName:DK_Register withAction:action complete:^{
            
    } fail:^{
            
    }];
}
#pragma mark -- TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url{
//    DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] init];
//    dk_WebViewCtrl.webUrl = url.absoluteString;
    
    DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] initWithUrl:url.absoluteString withIsNavBar:YES];
    [self.navigationController pushViewController:dk_WebViewCtrl animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
